import { Toast,Dialog  } from 'vant';

function formatNumber (n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}


export function formatTime (date,deadline=0) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()+ +deadline

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('/')
  const t2 = [hour, minute, second].map(formatNumber).join(':')

  return `${t1} ${t2}`
}
export function reverseData(data){
  let pattern=/^\d{4}-\d{2}-(\d{2})/
  return data.match(pattern)[1]
  
}

export function previewImage(current,urls){
  wx.previewImage({
    current: current, // 当前显示图片的http链接
    urls: urls, // 需要预览的图片http链接列表
    fail:err=>{console.log(err)}
  })
}

export function navigateTo(url){
  console.log(url);
  // wx.navigateTo({
  //   url:url
  // })
  this.$router.push({path:url})
}


export function redirectTo(url){
  console.log(url)
  wx.redirectTo({
    url:url,
    fail:err=>{
      console.log(err)
    }
  })
}

export function switchTab(url){
  console.log(url)
  wx.switchTab({
    url:url,
    fail:err=>{
      console.log(err)
    }
  })
}

export function sexTag(value){
  if(value=="男"){
    return 1
  }else if(value=="女"){
    return 2
  }else{
    return 0
  }
}

export function sexTag1(value){
  if(value==1){
    return '男'
  }else if(value==2){
    return '女'
  }else{
    return '男女不限'
  }
}

export function showToast(message='修改成功45646',time=1500,show='success'){
  if(show=='success'){
    Toast.success({
      message: message,
      duration:time
    })
  }else{
    Toast.fail({
      message: message,
      duration:time
    })
  }
}

// export function showModal(title,content){
//   var _this=this
//   let promise=new Promise((resolve ,reject) => {
//     wx.showModal({
//       title: title,
//       content: content,
//       success (res) {
//         if (res.confirm) {
//           resolve(true)
//         } else if (res.cancel) {
//           resolve(false)
//         }
//       },
//       fail:err=>{
//         reject(err)
//       }
//     })
//   })
//   return promise
// }

export function showModal(title,content){
    return Dialog.confirm({
      title: title,
      message: content
    }).then(() => {
      return true
    }).catch(()=>{
      return false
    })
    
}

export const mixin1={
  methods:{
    async getAddress(url='/other/get-address-all'){//vue mixin
      // let res=await this.$axios('/other/get-address-all');
      let res=await this.$axios(url);
      let address={};
      let province_list={};
      let city_list={};
      let county_list={};
          let n1=110000;
          
      if(res.data.level=='success'){//获取自主传入的地址
        let arr=[]
        res.data.data.forEach((val)=>{
          if(val.name!=''){
            province_list[n1]=val.name;
            if(val.cityList!=''){
              // debugger
              let n2=110;
              let city=val.cityList;
              city.forEach(val=>{
                city_list[n1+n2]=val.name;
                if(val.areaList!=''){
                  let n3=10;
                  val.areaList.forEach(val=>{
                    let obj={};
                    county_list[n1+n2+n3]=val.name;
                    obj[val.name]=val.townList
                    n3+=10
                    arr.push(obj);
                  })
                  n2+=100
                  // this.$store.dispatch('setTownList',arr)//存入区对应的镇或街道
                }
              })
              
            }
            n1+=10000;
           
          }
          this.$store.dispatch('setTownList',arr)//存入区对应的镇或街道;
        })
        
      }
      this.areaList.province_list=province_list;
      this.areaList.city_list=city_list;
      this.areaList.county_list=county_list;

    }
  }
}



export function upLoadImage(url){
  let promise=new Promise((resolve,reject)=>{
    axios.post({
      url:"https://wx.zhaowea.com/api/file/upload-image"
    })
    
  })
}
export function trim(str){//去除前后空格
  let strn=str.replace(/(^\s*)|(\s*$)/g,'')
  return strn
}

export default {
  formatNumber,
  formatTime,
  navigateTo,
  showToast,
  redirectTo,
  upLoadImage,
  previewImage,
  switchTab,
  sexTag,
  sexTag1,
  showModal,
  trim,
  reverseData,
  mixin1
}


